﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageDirectory : System.Web.UI.Page
{

    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_DIRECTORY = "__Directory::DC";
    private const string PROP_LAYOUT = "__Directory::UIL";
    private const string PROP_SELECTED_DIRECTORY_ID = "__Directory::SDCID";

    #endregion

    #region Public Properties

    public DirectoryCollection DataSourceDirectory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_DIRECTORY] is DirectoryCollection ? (DirectoryCollection)SessionMgr.Session[PROP_DATASRC_DIRECTORY] : (DirectoryCollection)(SessionMgr.Session[PROP_DATASRC_DIRECTORY] = new DirectoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_DIRECTORY] = value; }
    }

    public int SelectedDirectoryID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_DIRECTORY_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_DIRECTORY_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_DIRECTORY_ID] = value; }
    }


    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }


    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceDirectory = Directory.GetDirectoryByActiveStatus(false);
            NonActiveDirectoryList1.DataBind(DataSourceDirectory);
            DataSourceDirectory = Directory.GetDirectoryByActiveStatus(true);
            DirectoryIsActiveList1.DataBind(DataSourceDirectory);
        }
    }

    #endregion

    #region Control: NonActiveDirectoryList

    protected void ucNonActiveDirectoryList_EditRequested(object sender, Controls_NonActiveDirectoryList.EditRequestedEventArgs evt)
    {
        SelectedDirectoryID = evt.DC.UserID;
        DirectoryViewEdit1.DataBind(evt.DC);
        SetLayout(LayoutType.Edit);
    }


    protected void ucNonActiveDirectoryList_AddNewRequested(object sender, EventArgs evt)
    {
        //DirectoryViewEdit1.CancelEdit();
        //Directory newDirectory = Directory.CreateNew(DataSourceDirectory);
        //SelectedDirectoryID = newDirectory.UserID;
        //DirectoryViewEdit1.DataBind(DataSourceDirectory.FindByUserID(SelectedDirectoryID));
        //SetLayout(LayoutType.Edit);
    }


    protected void ucNonActiveDirectoryList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceDirectory = Directory.GetDirectoryByActiveStatus(false);
        NonActiveDirectoryList1.DataSource = DataSourceDirectory;
    }

    #endregion


    #region Control: ActiveDirectoryList

    protected void ucDirectoryIsActiveList_EditRequested(object sender, Controls_DirectoryIsActiveList.EditRequestedEventArgs evt)
    {
        SelectedDirectoryID = evt.DAl.UserID;
        DirectoryViewEdit1.DataBind(evt.DAl);
        SetLayout(LayoutType.Edit);
    }


    protected void ucDirectoryIsActiveList_AddNewRequested(object sender, EventArgs evt)
    {
        //DirectoryViewEdit1.CancelEdit();
        //Directory newDirectory = Directory.CreateNew(DataSourceDirectory);
        //SelectedDirectoryID = newDirectory.UserID;
        //DirectoryViewEdit1.DataBind(DataSourceDirectory.FindByUserID(SelectedDIRECTORYID));
        //SetLayout(LayoutType.Edit);
    }


    protected void ucDirectoryIsActiveList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceDirectory = Directory.GetDirectoryByActiveStatus(true);
        DirectoryIsActiveList1.DataSource = DataSourceDirectory;
    }

    #endregion

    #region Control: DirectoryViewEdit

    protected void ucDirectoryViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        DirectoryViewEdit1.DataSourceDirectory = DataSourceDirectory.FindByUserID(SelectedDirectoryID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        DirectoryViewEdit1.CancelEdit();
        // If we were adding a new Directory...then remove the incomplete/not added new Directory object from the collection.
        if (DataSourceDirectory.FindByUserID(SelectedDirectoryID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceDirectory.Remove(DataSourceDirectory.FindByUserID(SelectedDirectoryID));
                SelectedDirectoryID = (DataSourceDirectory[0] != null ? DataSourceDirectory[0].UserID : 0);
            }
            catch
            {
            }
        }
        NonActiveDirectoryList1.DataBind(DataSourceDirectory);
        SetLayout(LayoutType.View);
    }
    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {

        if (DirectoryViewEdit1.EndEdit())
        {

            Directory obj = DataSourceDirectory.FindByUserID(SelectedDirectoryID);
            Directory.Update(obj);

            // TODO: Set Feedback text...

            DataSourceDirectory = Directory.GetDirectoryByActiveStatus(false);
            NonActiveDirectoryList1.DataBind(DataSourceDirectory);
            DataSourceDirectory = Directory.GetDirectoryByActiveStatus(true);
            DirectoryIsActiveList1.DataBind(DataSourceDirectory);
            Response.AddHeader("Refresh", "0");
            SetLayout(LayoutType.View);

        }
        else
        {
            divexistbusinesscategoryname.Visible = true;
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlNonActiveDirectoryList.Visible = false;
                pnlDirectoryIsActiveList.Visible = false;
                pnlDirectoryViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:
            case LayoutType.Search:

                pnlNonActiveDirectoryList.Visible = true;
                pnlDirectoryIsActiveList.Visible = true;
                pnlDirectoryViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}

